// pages/cart/cart.js
import { Cart } from '../cart/cart-model.js';
var cart = new Cart();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loadingHidden: false,
    account:0,//商品总价
    selectedCounts: 0, //总的商品数
    selectedTypeCounts: 0, //总的商品类型数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /*离开页面时，更新本地缓存*/
  onHide: function () {
    cart.execSetStorageSync(this.data.cartData);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //获取缓存中的商品
    var cartData = cart.getCartDataFromLocal();
    
    //获取选中的商品数量
    //var countsInfo = cart.getCartTotalCounts(true);
    var cal = this._calcTotalAccountAndCounts(cartData);
    this.setData({
      cartData : cartData,
      account: cal.account,
      selectedCounts: cal.selectedCounts,
      selectedTypeCounts: cal.selectedTypeCounts,
      loadingHidden: true,
    });

  },

  /*
    * 计算总金额和选择的商品总数
    * */
  _calcTotalAccountAndCounts: function (data) {
    var len = data.length,
        //商品的总价，排除未选中的
        account = 0,
        //选中商品的数量
        selectedCounts = 0,
        //选中商品的类目数量
        selectedTypeCounts = 0;
        let multiple = 100;
        for(let i=0;i<len;i++){
          //避免 0.05 + 0.01 = 0.060 000 000 000 000 005 的问题，乘以 100 *100
          if (data[i].selectStatus) {
            account += data[i].counts * multiple * Number(data[i].price) * multiple;
            selectedCounts += data[i].counts;
            selectedTypeCounts++;
          }
        }
        return {
          selectedCounts: selectedCounts,
          selectedTypeCounts: selectedTypeCounts,
          account: account / (multiple * multiple)
        }
  },
  /**
   * 点击选中事件
   */
  toggleSelect:function(event){
    var id = cart.getDataSet(event, 'id'),
      status = cart.getDataSet(event,'status'),
    index = this._getProductIndexById(id);
    //改变状态
    this.data.cartData[index].selectStatus = !status;
    
    //重新计算商品
    this._restCartData();
  },
  /**
   * 全选按钮事件
   */
  toggleSelectAll:function(event){
    var status = cart.getDataSet(event, 'status') == 'true';
    var data = this.data.cartData;
    for (let i = 0; i < data.length; i++) {
      data[i].selectStatus = !status;
    }
    //重新计算商品
    this._restCartData();
  },
  /*根据商品id得到 商品所在下标*/
  _getProductIndexById:function(id){
    var data = this.data.cartData;
    for(let i=0;i<data.length;i++){
      if(data[i].id == id){
        return i;
      }
    }
  },
  /**
   * 重新计算商品价格
   */
  _restCartData:function(){
    var newCartData = this._calcTotalAccountAndCounts(this.data.cartData);
    this.setData({
      account:newCartData.account,
      cartData: this.data.cartData,
      selectedCounts: newCartData.selectedCounts,
      selectedTypeCounts: newCartData.selectedTypeCounts
    });
  },
  changeCounts:function(event){
    var id = cart.getDataSet(event, 'id'),
        type = cart.getDataSet(event, 'type'),
        index = this._getProductIndexById(id),
        counts = 1;
    if(type == 'add'){
      //加操作
      cart.addCounts(id,counts);
    }else{
      counts = -1;
      cart.cutCounts(id,counts);
    }
    this.data.cartData[index].counts += counts;
    //重新计算商品
    this._restCartData();
  },
  /*删除商品*/
  delete: function (event) {
    var id = cart.getDataSet(event, 'id'),
      index = this._getProductIndexById(id);
    this.data.cartData.splice(index, 1);//删除某一项商品

    this._restCartData();
    cart.delete(id);  //内存中删除该商品
  },
  //点击跳转到商品详情页
  onProductsItemTap: function (event) {
    var id = cart.getDataSet(event, 'id');
    wx.navigateTo({
      url: '../product/product?id=' + id
    })
  },
  //下单按钮事件
  submitOrder:function(event){
    wx.navigateTo({
      url: '../order/order?account=' + this.data.account + '&from=cart'
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})